<template>
    <div>
        <Tables 
            :title ='title' 
            :butns = 'butns' 
            :field = 'field' 
            :tableData ='tableData' 
            :perNum='per_num' 
            :totalNum="total_num" 
            :collectField = "collect_field"
            :curPage ="cur_page" 
            :clearSelectItems="selectedItems"
            :rankable ="true"
            :pageSize='pageSize'
            @reset="reset" 
            @rankData = "rankData"
            @loadData ="getData"
            @sentSelectedItems="getSelectedItems"
            @sentEditData="getEditData"
            @changePageSize = 'changePageSize'
            >
            <div slot="filter">
                <flieImport :type="'staticaddr'" :title="title"  @getData='fileReload'></flieImport>
            </div>
        </Tables>
        <Modals 
            :id ="'modal'" 
            :title ='title' 
            :field = 'field' 
            :collectField = "collect_field"
            :status = "status" 
            :errorMsg="errorMsg" 
            :type="type"
            :selectedItems="selectedItems" 
            :editData = "edit_data"  
            @sentErrorMsg = "getErrorMsg"
            @addEvent ="addData" 
            @editEvent = "editData" 
            @delEvent = "delData"
            >
        </Modals>
    </div>
</template>
<script>
    import Tables from 'components/common/Tables'
    import Modals from 'components/common/Modals'
    import NetService from 'services/netService' 
    import PageData from 'libs/PageData'
    import flieImport from 'components/common/flieImport'
    let butns = [
         {
            type:'add',
            name:'添加',
            event:'addEvent',
            icon:'/static/img/add.png',
            class:'add_btn',
        },
        {
            type:'del',
            name:'删除',
            event:'delEvent',
            icon:'/static/img/delete.png',
            class:'del_btn',
        },
        {
            type:'edit',
            name:'编辑',
            event:'editEvent',
            icon:'/static/img/modpic.png',
            class:'edit_btn',
        }
    ]
    let field = [
        {
            type:'checkbox',
        },
        {
            type:'rank',
        },
        {
            type:'macinput',
            label:'MAC地址',
            name:'mac',          
            regex: /^[0-9A-Fa-f]{2}:[0-9A-Fa-f]{2}:[0-9A-Fa-f]{2}:[0-9A-Fa-f]{2}:[0-9A-Fa-f]{2}:[0-9A-Fa-f]{2}$/,
            requried:true, 
            remark:'*必填',
           
        },
        {
            type:'ipinput',
            label:'IP地址',
            name:'ip',
            regex:/^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$/,
            requried:true, 
            remark:'*必填'  
        },
        {
            type:'none',
            label:'备注',
            name:'description',
			editable:true,
            regex:/^[\w\u4e00-\u9fa5]{0,16}$/
        }
    ]
    export default {
		components:{
		    Tables,
            Modals,
            flieImport
	    },
        data() {
            return{
                title:'静态地址',
                butns:butns,
                field:field,
                tableData:[],
                per_num:25,
                total_num:1,
                cur_page:1,
                pageSize:[10, 20, 40, 80],
                size:10,
                collect_field:'id',
                status:"",
                errorMsg:'',
                type:'',
                selectedItems:[],
                edit_data:{},
                size:10,
                order:'',
                by:''
            }
        },
        created() {
            this.getData()
        },
        methods:{
            getSelectedItems(selectedItems) {
                this.selectedItems = selectedItems
            },
            getErrorMsg(msg) {
                this.errorMsg = msg
            },
            getEditData(editdata) {
                this.edit_data = editdata 
            },
            reset(type) {
                this.status = ""
                this.errorMsg = ''
                this.type = type
                $('.inputs_wrap .error_foramt').removeClass('error_foramt')
            }, 
            rankData(param){
                this.order = param.order
                this.by = param.by
                this.getData()
            },
            changePageSize (nowpage, size) {
                this.cur_page = nowpage
                this.size = size
                this.getData()
            }, 
            fileReload () {
                this.getData()
            },
            getData(){
                NetService.getBindIp(this.cur_page,this.size, this.order, this.by)
                .then((res)=>{
                    if(res.errcode === 0){
                        this.tableData = res['23'].data
                        this.total_num = res['23'].count
                    }
                })
            },
            addData(params){
                 let conf = `${params.mac}|${params.ip}`
                 this.status ='ing'
                 NetService.addBindIp(conf)
                .then((res)=>{      
                    if(res.errcode === 0){
                       this.status ='ok'
                       this.getData()
                       $('#modal').modal('hide')
                    }else {
                        this.status ='error'
                        this.errorMsg = res.errorMessage	
                    }
                })
            },
            editData(params){
                let conf = `${params.id}|${params.mac}|${params.ip}`
                this.status ='ing'
                NetService.editBindIp(conf)
                .then((res)=>{
                    if(res.errcode === 0){
                        this.status ='ok'                       
                        $('#modal').modal('hide')
                        this.getData()
                    }else {
                        this.status ='error'
                        this.errorMsg = res.errorMessage	
                    }
                })
            },
            delData(params){
                 this.status ='ing'        
                 NetService.delBindIp(params.ids)
                .then((res)=>{
                    if(res.errcode === 0){
                        this.status ='ok'
                        this.getData()
                        $('#modal').modal('hide')
                        this.selectedItems = []
                    }else {
                        this.status ='error'
                        this.errorMsg = res.errorMessage	
                    }
                })
            }
        }
    }
</script>